home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex11-4.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  2KB  |  80 lines

  1. // ex11-4.c -- Communication between lightweight processes
  2. //             with a SharedQueue
  3.  
  4. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex11-4.c,v 3.0 90/05/15 22:44:27 kgorlen Rel $
  5.  
  6. #define BASE StackProc
  7. #if BASE == StackProc
  8. #include "StackProc.h"
  9. #endif
  10. #if BASE == HeapProc
  11. #include "HeapProc.h"
  12. #endif
  13. #include "SharedQueue.h"
  14. #include "Scheduler.h"
  15. #include "String.h"
  16.  
  17. class TestProcess : public BASE {
  18. public:
  19.     TestProcess(const char* name, int pri, 
  20.                 SharedQueue& in, SharedQueue& out,
  21.                 stackTy* bot);
  22.     static TestProcess* create(const char* name, int pri, 
  23.                 SharedQueue& in, SharedQueue& out);
  24. };
  25.  
  26. TestProcess::TestProcess(const char* pname, int pri, 
  27.                          SharedQueue& in, SharedQueue& out,
  28.                          stackTy* bot) : 
  29.                          BASE(pname,bot,pri)
  30. {
  31.     // parent lwp yields so this lwp can start
  32.     if ( FORK() ) { Scheduler::yield(); return; }
  33.  
  34.     while (YES) {
  35.         Object* msg = in.next();
  36.         cout << className() << ": " << name() 
  37.              << " received " << *msg << endl;
  38.         out.nextPut(*msg);
  39.         cout << className() << ": " << name() 
  40.              << " sent " << *msg << endl;
  41.         }
  42.     // terminate to avoid return
  43.     terminate();
  44. }
  45. TestProcess* TestProcess::create(const char* pname, int pri, 
  46.                                  SharedQueue& in, SharedQueue& out)
  47. {
  48.     auto stackTy bottom;
  49.     return new TestProcess(pname, pri, in, out, &bottom);
  50. }
  51. main()
  52. {
  53.     // construct Scheduler
  54.     // and create main context with priority 0
  55.     MAIN_PROCESS(0);
  56.  
  57.     SharedQueue* q0 = new SharedQueue(2);
  58.     SharedQueue* qin = q0;
  59.     SharedQueue* qout;
  60.  
  61.     // start up lightweight processes coupled by SharedQueues: 
  62.     // qout for kTH Process is qin for (k+1)TH Process
  63.     for (register int i=1; i<=MAXPRIORITY; i++) {
  64.         String* pname = new String("P");
  65.         *pname &= (char)('0'+i);
  66.         qout = new SharedQueue(2);
  67.         TestProcess::create(*pname, i, *qin, *qout);
  68.         qin = qout;
  69.         }
  70.  
  71.     // put a message on the first input SharedQueue
  72.     String& inmsg = *new String("THE MESSAGE");
  73.     cout << "process Main sending: " << inmsg << endl;
  74.     q0->nextPut(inmsg);
  75.     
  76.     // main Process waits for message on last output SharedQueue
  77.     String& outmsg = *(String*)qout->next();
  78.     cout << "process Main received: " << outmsg << endl;
  79. }
  80.